package com.starlight.flower_server.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.starlight.flower_server.infrastructure.entity.Item;
import com.starlight.flower_server.infrastructure.entity.ItemCategory;
import com.starlight.flower_server.infrastructure.mapper.ItemCategoryMapper;
import com.starlight.flower_server.infrastructure.repository.ItemCategoryRepository;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author yfy2001
 * @date 2023/5/22 19:18
 */
@Repository
public class ItemCategoryRepositoryImpl implements ItemCategoryRepository {

    @Resource
    private ItemCategoryMapper itemCategoryMapper;

    @Override
    public ItemCategory queryDetail(Long id) {
        return itemCategoryMapper.selectById(id);
    }

    @Override
    public Integer insert(ItemCategory itemCategory) {
        String operator= CurrentUserInfoUtils.get("username");
        itemCategory.setCreator(operator);
        itemCategory.setModifier(operator);
        itemCategory.setCreated(new Date());
        itemCategory.setModified(new Date());
        itemCategory.setIsDeleted(0);
        return itemCategoryMapper.insert(itemCategory);
    }

    @Override
    public Integer modify(ItemCategory itemCategory) {
        String modifier= CurrentUserInfoUtils.get("username");
        itemCategory.setModifier(modifier);
        itemCategory.setModified(new Date());
        return itemCategoryMapper.updateById(itemCategory);
    }

    @Override
    public Integer logicDelete(Long id) {
        return itemCategoryMapper.deleteById(id);
    }

    @Override
    public List<ItemCategory> querySub(Long categoryId) {
        return itemCategoryMapper.selectList(new LambdaQueryWrapper<ItemCategory>()
                .eq(Objects.nonNull(categoryId),ItemCategory::getParentId,categoryId));
    }

    @Override
    public List<ItemCategory> queryAll() {
        return itemCategoryMapper.selectList(new LambdaQueryWrapper<ItemCategory>()
                .isNotNull(ItemCategory::getId));
    }
}
